﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Collections.Generic;//Please add references
namespace Hermes.DAL
{
	/// <summary>
	/// 数据访问类:OrderLines
	/// </summary>
	public partial class OrderLines
	{
		public OrderLines()
		{}
		#region  Method



		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Hermes.Model.OrderLines model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into OrderLines(");
			strSql.Append("OrderNUmber,SKU,EBaySKU,QTY,Price)");
			strSql.Append(" values (");
			strSql.Append("@OrderNUmber,@SKU,@EBaySKU,@QTY,@Price)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@OrderNUmber", SqlDbType.NVarChar,200),
					new SqlParameter("@SKU", SqlDbType.NVarChar,50),
					new SqlParameter("@EBaySKU", SqlDbType.NVarChar,50),
					new SqlParameter("@QTY", SqlDbType.Int,4),
					new SqlParameter("@Price", SqlDbType.Float,8)};
			parameters[0].Value = model.OrderNUmber;
			parameters[1].Value = model.SKU;
			parameters[2].Value = model.EBaySKU;
			parameters[3].Value = model.QTY;
			parameters[4].Value = model.Price;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Hermes.Model.OrderLines model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update OrderLines set ");
			strSql.Append("OrderNUmber=@OrderNUmber,");
			strSql.Append("SKU=@SKU,");
			strSql.Append("EBaySKU=@EBaySKU,");
			strSql.Append("QTY=@QTY,");
			strSql.Append("Price=@Price");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@OrderNUmber", SqlDbType.NVarChar,200),
					new SqlParameter("@SKU", SqlDbType.NVarChar,50),
					new SqlParameter("@EBaySKU", SqlDbType.NVarChar,50),
					new SqlParameter("@QTY", SqlDbType.Int,4),
					new SqlParameter("@Price", SqlDbType.Float,8),
					new SqlParameter("@ID", SqlDbType.Int,4)};
			parameters[0].Value = model.OrderNUmber;
			parameters[1].Value = model.SKU;
			parameters[2].Value = model.EBaySKU;
			parameters[3].Value = model.QTY;
			parameters[4].Value = model.Price;
			parameters[5].Value = model.ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from OrderLines ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
			parameters[0].Value = ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

        /// <summary>
        /// 删除一个订单的数据
        /// </summary>
        public bool Delete(string orderNumber)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from OrderLines ");
            strSql.Append(" where OrderNumber=@OrderNumber");
            SqlParameter[] parameters = {
					new SqlParameter("@OrderNumber", SqlDbType.NVarChar,200)
};
            parameters[0].Value = orderNumber;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from OrderLines ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Hermes.Model.OrderLines GetModel(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 ID,OrderNUmber,SKU,EBaySKU,QTY,Price,LastUpdate from OrderLines ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
			parameters[0].Value = ID;

			Hermes.Model.OrderLines model=new Hermes.Model.OrderLines();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ID"]!=null && ds.Tables[0].Rows[0]["ID"].ToString()!="")
				{
					model.ID=int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
				}
				
				if(ds.Tables[0].Rows[0]["OrderNUmber"]!=null && ds.Tables[0].Rows[0]["OrderNUmber"].ToString()!="")
				{
					model.OrderNUmber=ds.Tables[0].Rows[0]["OrderNUmber"].ToString();
				}
				if(ds.Tables[0].Rows[0]["SKU"]!=null && ds.Tables[0].Rows[0]["SKU"].ToString()!="")
				{
					model.SKU=ds.Tables[0].Rows[0]["SKU"].ToString();
				}
				if(ds.Tables[0].Rows[0]["EBaySKU"]!=null && ds.Tables[0].Rows[0]["EBaySKU"].ToString()!="")
				{
					model.EBaySKU=ds.Tables[0].Rows[0]["EBaySKU"].ToString();
				}
				if(ds.Tables[0].Rows[0]["QTY"]!=null && ds.Tables[0].Rows[0]["QTY"].ToString()!="")
				{
					model.QTY=int.Parse(ds.Tables[0].Rows[0]["QTY"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Price"]!=null && ds.Tables[0].Rows[0]["Price"].ToString()!="")
				{
					model.Price=decimal.Parse(ds.Tables[0].Rows[0]["Price"].ToString());
				}
                //if(ds.Tables[0].Rows[0]["LastUpdate"]!=null && ds.Tables[0].Rows[0]["LastUpdate"].ToString()!="")
                //{
                //    model.LastUpdate=DateTime.Parse(ds.Tables[0].Rows[0]["LastUpdate"].ToString());
                //}
				return model;
			}
			else
			{
				return null;
			}
		}


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public List<Hermes.Model.OrderLines> GetModels(string orderNumber)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  ID,OrderNUmber,SKU,EBaySKU,QTY,Price,LastUpdate from OrderLines ");
            strSql.Append(" where OrderNUmber=@OrderNumber");
            SqlParameter[] parameters = {
					new SqlParameter("@OrderNumber", SqlDbType.NVarChar,200)
};
            parameters[0].Value = orderNumber;

            List<Hermes.Model.OrderLines> models = new List<Model.OrderLines>();

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; ++i)
                {
                    Hermes.Model.OrderLines model = new Hermes.Model.OrderLines();
                    if (ds.Tables[0].Rows[i]["ID"] != null && ds.Tables[0].Rows[i]["ID"].ToString() != "")
                    {
                        model.ID = int.Parse(ds.Tables[0].Rows[i]["ID"].ToString());
                    }
                    
                    if (ds.Tables[0].Rows[i]["OrderNUmber"] != null && ds.Tables[0].Rows[i]["OrderNUmber"].ToString() != "")
                    {
                        model.OrderNUmber = ds.Tables[0].Rows[i]["OrderNUmber"].ToString();
                    }
                    if (ds.Tables[0].Rows[i]["SKU"] != null && ds.Tables[0].Rows[i]["SKU"].ToString() != "")
                    {
                        model.SKU = ds.Tables[0].Rows[i]["SKU"].ToString();
                    }
                    if (ds.Tables[0].Rows[i]["EBaySKU"] != null && ds.Tables[0].Rows[i]["EBaySKU"].ToString() != "")
                    {
                        model.EBaySKU = ds.Tables[0].Rows[i]["EBaySKU"].ToString();
                    }
                    if (ds.Tables[0].Rows[i]["QTY"] != null && ds.Tables[0].Rows[i]["QTY"].ToString() != "")
                    {
                        model.QTY = int.Parse(ds.Tables[0].Rows[i]["QTY"].ToString());
                    }
                    if (ds.Tables[0].Rows[i]["Price"] != null && ds.Tables[0].Rows[i]["Price"].ToString() != "")
                    {
                        model.Price = decimal.Parse(ds.Tables[0].Rows[i]["Price"].ToString());
                    }
                    //if (ds.Tables[0].Rows[i]["LastUpdate"] != null && ds.Tables[0].Rows[i]["LastUpdate"].ToString() != "")
                    //{
                    //    model.LastUpdate = DateTime.Parse(ds.Tables[0].Rows[i]["LastUpdate"].ToString());
                    //}

                    models.Add(model);
                }
            }
            else
            {
                return null;
            }
            return models;
        }

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,OrderNUmber,SKU,EBaySKU,QTY,Price,LastUpdate ");
			strSql.Append(" FROM OrderLines ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" ID,OrderNUmber,SKU,EBaySKU,QTY,Price,LastUpdate ");
			strSql.Append(" FROM OrderLines ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "OrderLines";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

